package com.yx.frame.builder.code;

import java.io.OutputStreamWriter;

public class JsFileWriter {
	static long idNumber=0;
	OutputStreamWriter writer;
	OutputStreamWriter jsAppWriter;
	OutputStreamWriter docWriter;
	public JsFileWriter(OutputStreamWriter writer,OutputStreamWriter jsAppWriter,OutputStreamWriter docWriter) {
		this.writer=writer;
		this.docWriter=docWriter;
		this.jsAppWriter=jsAppWriter;
	}
	public void write(String isAPP,String jsStr)throws Exception{
		this.writer.write(jsStr);
		if("true".equals(isAPP)){
			this.jsAppWriter.write(jsStr);
		}
	}
	public void flush()throws Exception{
		this.writer.flush();
		this.jsAppWriter.flush();
		this.docWriter.flush();
	}
	public void close()throws Exception{		
		this.docWriter.write("</body></html>");
		this.docWriter.flush();
		this.jsAppWriter.close();
		this.writer.close();
		this.docWriter.close();
	}
	public static StringBuffer htmlStr(String isAPP,String id,String methodName,String discription,String author,String riqi,String parmsInput,String returnOutput,String sample)throws Exception{
		if("true".equals(isAPP))discription="<font color=red>[手机APP]</font>"+discription;
		StringBuffer sb=new StringBuffer();
		String divId="div_display_"+idNumber++;
		sb.append("<table align=\"left\" width=\"1200\" border=\"1\" cellpadding=\"5\" cellspacing=\"0\" >\r\n");	
		sb.append("<tr align=\"left\">\r\n");			
		sb.append("<td width=\"15%\" colspan=\"6\" style=\"font-size:18px;\"><a href=\"javascript:table_"+divId+"_show()\" >."+methodName+"</a>:"+discription+"</td>\r\n");	
		sb.append("</tr>\r\n");
		sb.append("</table>\r\n");	
		sb.append("<script>\r\n");
		sb.append("table_"+divId+"_show = function()\r\n");
		sb.append("{\r\n");
		sb.append("if (document.getElementById(\""+divId+"\").style.display == \"none\") {    \r\n"); 
		sb.append("		document.getElementById(\""+divId+"\").style.display = \"\";  \r\n");   
		sb.append("	}\r\n");
		sb.append("	else{ \r\n");    
		sb.append("		document.getElementById(\""+divId+"\").style.display = \"none\"; \r\n");    
		sb.append("	}  \r\n");  			
		sb.append("}\r\n");
		sb.append("</script> \r\n");
		sb.append("<div id=\""+divId+"\" style=\"display:none\">\r\n");
		sb.append("<table align=\"left\" width=\"1200\" border=\"1\" cellpadding=\"5\" cellspacing=\"0\" >\r\n");	
		sb.append("<tr align=\"left\">\r\n");	
		sb.append("<td width=\"15%\" >编号</td>\r\n");	
		sb.append("<td width=\"15%\" >"+id+"</td>\r\n");	
		sb.append("<td width=\"15%\" >作者</td>\r\n");	     
		sb.append("<td width=\"20%\" >"+author+"</td>\r\n");	
		sb.append("<td width=\"15%\" >日期</td>\r\n");	
		sb.append("<td width=\"20%\">"+riqi+"</td>\r\n");	
		sb.append("</tr>\r\n");		
		sb.append("<tr align=\"left\">\r\n");	
		sb.append("<td width=\"15%\" >传入参数</td>\r\n");	
		sb.append("<td width=\"90%\"colspan=\"5\" >"+parmsInput+"</td>\r\n");	
		sb.append("</tr>\r\n");	
		sb.append("<tr align=\"left\">\r\n");	
		sb.append("<td width=\"15%\" >返回结果</td>\r\n");	
		sb.append("<td width=\"90%\"colspan=\"5\" >\r\n");	
		sb.append(returnOutput);	
		sb.append("</td>\r\n");	
		sb.append("</tr>\r\n");	
		sb.append("<tr align=\"left\">\r\n");	
		sb.append("<td width=\"15%\" >调用示例</td>\r\n");		
		sb.append("<td width=\"90%\"colspan=\"5\" >\r\n");	
		sb.append(sample);
		sb.append("</td>\r\n");	
		sb.append("</tr>\r\n");		
		sb.append("</table>\r\n");	
		sb.append("</div>\r\n");
		return sb;		
	}
	public void htmlWrite(String s)throws Exception{
		this.docWriter.write(s);		
	}
	public void html(String isAPP,String id,String methodName,String discription,String author,String riqi,String parmsInput,String returnOutput,String sample)throws Exception{
		this.docWriter.write(htmlStr(isAPP,id,methodName,discription,author,riqi,parmsInput,returnOutput,sample).toString());
		this.docWriter.flush();
	}
}
